<?php
require_once 'conexao.php';
?>
<html>
    <body>
        <table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 1210px; height: 93px;">

            <tr>
                <td style="width: 200px; height: 70px;">
                    <img alt="" src="http://www.saoexpedito.com.br/imagens/logo.jpg" style="width: 201px; height: 70px;" /></td>
                <td style="text-align: center;"><h1>CONTROLE DE ENTRADA E SAÍDA - VISITANTE</h1></td>
                <td style="width: 150px;">
                    Rev.: 03<br />
                    Data: 11/05/2010<br />
                    P&aacute;gina 1 de 1</td>
            </tr>

        </table> 
        <br /><br /><br /><br /><br /><br />
        <?php
        $i = explode('/', $_POST['dt_entrada_ini']);
        $i = $i['2'] . "-" . $i['1'] . "-" . $i['0'];
        $entrada_fim = explode('/', $_POST['dt_entrada_fim']);
        $entrada_fim = $entrada_fim['2'] . "-" . $entrada_fim['1'] . "-" . $entrada_fim['0'];
        while ($i <= $entrada_fim) {
            $sql = "SELECT visitante.*, cadastro.nome, cadastro.empresa FROM visitante, cadastro WHERE cadastro.rg = visitante.rg and dt_entrada ='" . $i . "'";

            if ($_POST['rg'] <> 'TODOS') {
                $sql = $sql . " and visitante.rg = " . $_POST['rg'];
            }
            if ($_POST['cracha'] <> 'TODOS') {
                $sql = $sql . " and cracha =" . $_POST['cracha'];
            }
            if ($_POST['setor'] <> 'TODOS') {
                $sql = $sql . " and setor = '" . $_POST['setor'] . "'";
            }
            if ($_POST['autorizacao'] <> 'TODOS') {
                $sql = $sql . " and autorizacao = '%" . $_POST['autorizacao'] . "%'";
            }
            if ($_POST['obs'] <> 'TODOS') {
                $sql = $sql . " and obs LIKE '%" . $_POST['obs'] . "%'";
            }
            if ($_POST['nome'] <> 'TODOS') {
                $sql = $sql . " and cadastro.nome LIKE '%" . $_POST['nome'] . "%'";
            }
            if ($_POST['empresa'] <> 'TODOS') {
                $sql = $sql . " and empresa LIKE '%" . $_POST['empresa'] . "%'";
            }
            $result = mysql_query($sql);
            echo mysql_error();
            if (mysql_num_rows($result) > 0) {
 
                $aux = explode('-', $i);
                $aux = $aux['2'] . "/" . $aux['1'] . "/" . $aux['0'];
                echo "<b>Data: " . $aux . "</b>";
                ?> 

                <table width="1210" border="0" name="tabela_rel">
                    <tr style="background-color: lightgray; font-weight: bold">
                        <td width="106" align="center">RG</td>
                        <td width="363" align="center">NOME</td>
                        <td width="124" align="center">CRACHA</td>
                        <td width="193" align="center">SETOR</td>
                        <td width="78" align="center">AUTORIZAÇÃO</td>
                        <td width="101" align="center">EMPRESA</td>
                        <td width="198" align="center">HR ENTRADA</td>
                        <td width="198" align="center">HR SAÍDA</td>
                        <td width="198" align="center">OBSERVAÇÃO</td>
                        <td width="198" align="center">ENTRADA</td>
                        <td width="198" align="center">SAÍDA</td>
                    </tr>
                    <?php
                    echo mysql_error();
                    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                        if ($row['hr_saida'] == '00:00:00')
                            $row['hr_saida'] = '-';
                        if ($row['obs'] == ' ' OR $row['obs'] == '' OR !isset($row['obs']))
                            $row['obs'] = '-';
                        if ($row['empresa'] == ' ' OR $row['empresa'] == '' OR !isset($row['empresa']))
                            $row['empresa'] = '-';
                        echo"<tr>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . ($row['rg']) .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . ($row['nome']) .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['cracha'] .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['setor'] .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['permissao'] .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['empresa'] .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['hr_entrada'] .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['hr_saida'] .
                        "</td>       
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['obs'] .
                        "</td>       
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['port_ent'] .
                        "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['port_saida'] .
                        "</td>
                    </tr>";
                    }
                    echo "</table> <br /><br />";
                }
                $i = date('Y-m-d', strtotime("+1 days", strtotime($i)));
            }
            ?>

    </body>
</html>